package com.example.demo.service.Impl;
import com.example.demo.bean.Article;
import com.example.demo.bean.ArticleExample;

import com.example.demo.dao.ArticleMapper;
import com.example.demo.service.IArticleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
@Service
public class ArticleServiceImpl implements IArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Override
    public List<Article> query(String title, String status) {
        ArticleExample example=new ArticleExample();//模板
        if(!StringUtils.isEmpty(title)){
            example.createCriteria().andTitleLike("%"+title+"%");//模糊查询
        }
        if(!StringUtils.isEmpty(status)){
            example.createCriteria().andStatusEqualTo(status);
        }
        List<Article> list=articleMapper.selectByExample(example);
        return list;
    }

    @Override
    public void saveOrUpdate(Article article) {
        if(article.getId()==null){
            articleMapper.insert(article);
        }else{
            articleMapper.updateByPrimaryKey(article);
        }
    }

    @Override
    public void deleteById(long id) {
        articleMapper.deleteByPrimaryKey(id);
    }
}
